//
//  YYETabBar.swift
//  weibo14
//
//  Created by mac on 16/3/2.
//  Copyright © 2016年 mac. All rights reserved.
//  目的为添加按钮

import UIKit

class YYETabBar: UITabBar {

    
    // 问老师 不然弄成let
    var callBack:(()->())?
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
    
        backgroundImage = UIImage(named: "tabbar_background")
        
        addSubview(composeButton)
    }

    override func layoutSubviews() {
         super.layoutSubviews()
        
        var index = 0
        
        let buttonW = self.frame.width / 5
        
        
        for value in self.subviews {
            
            // 判断vale是否是 button
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!) == true {
            
                let buttonX = CGFloat(index) * buttonW
            
                value.frame.origin.x = buttonX
                
                value.frame.size.width = buttonW
            
                index++
                
                if index == 2 {
                
                    index++
                }
                
            }
        }
        
        // 调整加号的位置
        
//        composeButton.center = CGPoint(x: self.center.x, y: self.frame.size.width *0.5)
        // x 是这个框框 中心的x的坐标
        // y 是高的一半,也是中心点的一半
        
        composeButton.center = CGPoint(x: self.center.x, y: self.frame.height * 0.5)


    }
    
    lazy var composeButton :UIButton = {
    
        let button = UIButton()
       
        
        // 添加相应函数
        button.addTarget(self, action: "click", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
    
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        // 设置图片
 
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState:UIControlState.Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        button.sizeToFit()
        
        return button
    }() //这个括号要注意了
    
    
    
    // private 这个一定不能加 明天问老师
    func click(){
    
//      print("!!")
        callBack?()
        
        
    }
    
    
    
    
    
}
